package com.shy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shy.config.common.BaseQuery;
import com.shy.config.util.AuthUtils;
import com.shy.entity.User;
import com.shy.mapper.UserMapper;
import com.shy.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * 根据id查询用户
     *
     * @param userId id
     * @return
     */
    @Override
    public User queryUserById(Integer userId) {
        return userMapper.selectByPrimaryKey(Long.valueOf(userId));
    }

    @Override
    public User queryUserDetailById(Integer userId) {
        return userMapper.queryUserDetailById(userId);
    }

    /**
     * 验证旧密码是否一致
     *
     * @param oldPwd 密码
     * @return
     */
    @Override
    public Boolean checkLoginPwd(String oldPwd) {
        User user = userMapper.selectByPrimaryKey(Long.valueOf(AuthUtils.getLoginSecurityUserId()));
        return passwordEncoder.matches(oldPwd, user.getLoginPwd());
    }

    /**
     * 修改密码
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public Boolean modifyPwd(User user) {
        user.setId(AuthUtils.getLoginSecurityUserId());
        user.setLoginPwd(passwordEncoder.encode(user.getLoginPwd()));
        user.setEditBy(AuthUtils.getLoginSecurityUserId());
        user.setEditTime(new Date());
        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    /**
     * 分页查询用户
     *
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<User> queryUserListByPage(Integer current, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(current, pageSize);
        //查询数据
        List<User> list = userMapper.queryUserListByPage(new BaseQuery());
        //创建分页对象
        PageInfo<User> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public Boolean modifyUser(User user) {
        // 设置修改者ID为当前登录安全用户的ID
        user.setEditBy(AuthUtils.getLoginSecurityUserId());
        // 设置修改时间为当前时间
        user.setEditTime(new Date());
        // 返回用户信息更新操作是否成功
        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @Override
    public Boolean deleteUserById(Integer userId) {
        return userMapper.deleteByPrimaryKey(Long.valueOf(userId)) > 0;
    }

    /**
     * 添加用户
     */
    @Override
    public Boolean addUser(User user) {
        // 设置用户的创建者ID，使用当前登录的安全用户ID
        user.setCreateBy(AuthUtils.getLoginSecurityUserId());
        // 设置用户的创建时间，使用当前系统时间
        user.setCreateTime(new Date());
        // 设置用户的登录密码，使用密码编码器进行加密处理以增强安全性
        user.setLoginPwd(passwordEncoder.encode(user.getLoginPwd()));
        return userMapper.insertSelective(user) > 0;
    }
    /**
     * 批量删除
     */
    @Override
    public Boolean batchDelete(List<Integer> userIds) {
        return userMapper.batchDelete(userIds) > 0;
    }
}

